package org.Utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class ReadResourceFile {
    static BufferedReader read(String filePath) {
        BufferedReader reader = null;
        try {
            // 获取resources文件夹下的文件URL
            URL resource = ReadResourceFile.class.getResource(filePath);

            // 获取文件输入流
            InputStream inputStream = resource.openStream();

            // 创建BufferedReader对象,用于读取输入流中的内容
            reader = new BufferedReader(new InputStreamReader(inputStream));

            // 读取文件内容
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            // 关闭BufferedReader对象
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return reader;
        }
    }

    // 获取resources根路径
    public static String getPath(String path) {
        // 获取resources文件夹根路径
        URL resource = ReadResourceFile.class.getResource("/");
        if (resource != null) {
            String rootPath = resource.getPath();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(rootPath);
            stringBuilder.append(path);
//            System.out.println("rootPath = " + stringBuilder.toString());
            return stringBuilder.toString();
        } else {
            return null;
        }
    }
}
